package com.ruoyi.system.api;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.domain.SysUserRole;
import io.seata.spring.annotation.GlobalLock;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.constant.ServiceNameConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.factory.RemoteUserFallbackFactory;
import com.ruoyi.system.api.model.LoginUser;

/**
 * 用户服务
 *
 * @author ruoyi
 */
@FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.SYSTEM_SERVICE
        , fallbackFactory = RemoteUserFallbackFactory.class)
public interface RemoteUserService {
    /**
     * 通过用户名查询用户信息
     *
     * @param username 用户名
     * @param source   请求来源
     * @return 结果
     */
    @GetMapping("/user/info/{username}")
    public R<LoginUser> getUserInfo(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);

    @GetMapping("/user/infoByUsername")
    R<SysUser> getUserByUserName(@RequestParam("username") String username,
                                 @RequestHeader(SecurityConstants.FROM_SOURCE) String source);

    /**
     * 注册用户信息
     *
     * @param sysUser 用户信息
     * @param source  请求来源
     * @return 结果
     */
    @Transactional
    @GlobalLock
    @PostMapping("/user/register")
    public R<Boolean> registerUserInfo(@RequestBody SysUser sysUser, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);

    /**
     * 记录用户登录IP地址和登录时间
     *
     * @param sysUser 用户信息
     * @param source  请求来源
     * @return 结果
     */
    @PutMapping("/user/recordlogin")
    public R<Boolean> recordUserLogin(@RequestBody SysUser sysUser, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);

    /**
     * 授予用户商户角色
     *
     * @param userId
     * @param roleIds
     * @param source
     * @return
     */
    @PutMapping("/user/authRole")
    public AjaxResult insertAuthRole(@RequestParam("userId") Long userId, @RequestParam("roleIds") Long[] roleIds, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);


    @GetMapping("/user/getUserByphone")
    public SysUser getUserByphone(String phonenumber);

    @PostMapping("/users")
    public AjaxResult add(@RequestBody SysUser sysUser);


    @PostMapping("/userRole/add")
    public R add(@RequestBody SysUserRole sysUserRole);

    /**
     * 通过手机号获取用户信息
     *
     * @param phonenumber
     * @param source
     * @return
     */
    @GetMapping("/user/getUserByPhonenumber")
    public R<LoginUser> getUserByPhonenumber(@RequestParam("phonenumber") String phonenumber,
                                             @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
}
